/*
 *  AMPImgAssignmentDlgController.cpp
 *  AMPlugin
 *
 *  Created by Vu Tiet on 8/16/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */



#include "VCPlugInHeaders.h"

// Interface includes:
#include "IActiveContext.h"
#include "ITextEditSuite.h" 
#include "ISelectionManager.h"
#include "IPanelControlData.h" 
#include "IDropDownListController.h"
#include "IStringListData.h"
#include "ITreeViewHierarchyAdapter.h"
#include "ITreeViewMgr.h"


// General includes:
#include "CDialogController.h"
#include "StringUtils.h"
// Project includes:
#include <string>
#include "AMPID.h"
#include "HTTPRequestHelper.h"
#include "AMGlobal.h"

/**
 Methods allow for the initialization, validation, and application of dialog widget
 values.
 Implements IDialogController based on the partial implementation CDialogController.
 
 
 @ingroup writefishprice
 */
class AMPImgAssignmentDlgController : public CDialogController
	{
	public:
		/** Constructor.
		 @param boss interface ptr from boss object on which this interface is aggregated.
		 */
		AMPImgAssignmentDlgController(IPMUnknown* boss) : CDialogController(boss) {}
		
		/** Destructor.
		 */
		virtual ~AMPImgAssignmentDlgController() {}
		
		/** Initialize each widget in the dialog with its default value.
		 Called when the dialog is opened.
		 */
	    virtual void InitializeDialogFields(IActiveContext* dlgContext);
		
		/** Validate the values in the widgets.
		 By default, the widget with ID kOKButtonWidgetID causes
		 ValidateFields to be called. When all widgets are valid,
		 ApplyFields will be called.
		 @return kDefaultWidgetId if all widget values are valid, WidgetID of the widget to select otherwise.
		 
		 */
		virtual WidgetID ValidateDialogFields(IActiveContext* myContext);
		
		
		/** Retrieve the values from the widgets and act on them.
		 @param widgetId identifies the widget on which to act.
		 */
		virtual void ApplyDialogFields(IActiveContext* myContext, const WidgetID& widgetId);
	};

CREATE_PMINTERFACE(AMPImgAssignmentDlgController, kAMPImgAssignmentDlgControllerImpl)

/* ApplyFields
 */
void AMPImgAssignmentDlgController::InitializeDialogFields(IActiveContext* dlgContext)
{
	CDialogController::InitializeDialogFields(dlgContext);
	////cout << "AMPImgAssignmentDlgController initialize" << endl;

	string imgCaption = "";
	for (int i = 0; i < g_ImageInfo.size(); i++) {
		images_t iImgInfo = g_ImageInfo[i];
		////cout << "iImgInfo.image_path & g_ChosenImagePath: " << iImgInfo.image_path << " & " << g_ChosenImagePath << endl;
		if (iImgInfo.image_path == g_ChosenImagePath) {
			imgCaption = iImgInfo.image_caption;
			break;
		}
	}
	
	PMString fillStr(imgCaption.c_str());
	SetTextControlData(kAMPImgCaptionEditTextWidgetID, fillStr);
	////cout << "set text to image caption text box" << endl;
	
	do {
		InterfacePtr<IPanelControlData> panelData(this, UseDefaultIID());	
		// Don't assert, fail silently, the tree view panel may be closed.
		if(panelData == nil)
		{
			break;
		}
		
		IControlView* treeWidget = panelData->FindWidget(kAMPStoryTreeViewWidgetID);
		ASSERT(treeWidget);
		if(treeWidget == nil)
		{
			break;
		}
		
		
		// create new data list
		InterfacePtr<ITreeViewHierarchyAdapter> model(treeWidget, UseDefaultIID());
		ASSERT(model);
		if(!model) {
			break;
		}
		
		K2Vector<PMString> lists;
		
		for (int i = 0; i < g_assigneeSt.size(); i++) {
			string listItem = g_assigneeSt[i].storyID;
			//cout << "create new data list item: " << listItem << endl;
			PMString itemPMS(listItem.c_str());
			itemPMS.Translate();
			lists.push_back(itemPMS);
		}
		
		InterfacePtr<IStringListData> iListData(model, IID_ISTRINGLISTDATA);
		iListData->SetStringList(lists);
		
		// redraw UI
		InterfacePtr<ITreeViewMgr> treeMgr(treeWidget, UseDefaultIID());
		
		treeMgr->ClearTree();
		treeMgr->ChangeRoot(kTrue);		
	} while(kFalse);

}

/* ValidateFields
 */
WidgetID AMPImgAssignmentDlgController::ValidateDialogFields(IActiveContext* myContext)
{
	
	WidgetID result = CDialogController::ValidateDialogFields(myContext);
	// Put code to validate widget values here.
	return result;
}

/* ApplyFields
 */
void AMPImgAssignmentDlgController::ApplyDialogFields(IActiveContext* myContext, const WidgetID& widgetId)
{
	// do nothing
	
	
}
